<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品货号">
          <a-auto-complete
            v-decorator="['productSn', validatorRules.productSn]"
            :data-source="productSnDataSource"
            placeholder="请输入商品货号"
            :filter-option="filterOption"
            @select="onSelect"
            :disabled="editEnable"
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="物料编码">
          <a-input placeholder="请输入物料编码" v-decorator="['skuCode', validatorRules.skuCode ]" :disabled="editEnable"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="物料编码">
          <a-input placeholder="请输入物料编码" v-decorator="['materialCode', validatorRules.materialCode ]" :disabled="editEnable"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="物料名称">
          <a-input placeholder="请输入物料名称" v-decorator="['materialName', validatorRules.materialName ]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="成本价">
          <a-input-number v-decorator="[ 'costPrice', validatorRules.costPrice]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开始启用时间">
          <a-date-picker format="YYYY-MM-DD HH:mm:ss" :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }" v-decorator="[ 'startTime', validatorRules.startTime]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'

  export default {
    name: 'StmSkuMaterialModal',
    data() {
      return {
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        editEnable: true,
        productSnDataSource: [],
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          productSn: { rules: [{ required: true, message: '请选择货号!' }] },
          skuCode: { rules: [{ required: true, message: '请输入物料编码!' }] },
          materialCode: { rules: [{ required: true, message: '请输入物料编码!' }] },
          costPrice: { rules: [{ required: true, message: '请输入成本价!' }] },
          startTime: { rules: [{ required: true, message: '请选择开始启用时间!' }] },
          materialName: { rules: [{ required: true, message: '请输入物料名称!' }] },
        },
        url: {
          add: '/stm/stmSkuMaterial/add',
          edit: '/stm/stmSkuMaterial/edit',
          queryList: '/pms/pmsProduct/queryList',
          fromProductSnToSkuCode: '/pms/pmsProduct/fromProductSnToSkuCode'
        }
      }
    },
    created() {
    },
    methods: {
      moment,
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.editEnable = true;
        if(this.model.id === undefined || this.model.id === null || this.model.id === '' ){
          this.editEnable = false;
        }
        this.visible = true
        this.getProduct()
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'productSn', 'skuCode', 'materialCode', 'materialName', 'costPrice', 'startTime'))
          //时间格式化
          this.form.setFieldsValue({ startTime: this.model.startTime ? moment(this.model.startTime) : null })
        })

      },
      getProduct() {
        getAction(this.url.queryList).then(res => {
          if (res.success) {
            console.log('ProductData => ' + res.result)
            this.productSnDataSource = [...new Set(res.result.map(data => (data['productSn'])))]
          } else {
            console.log(res.message)
          }
        })
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLocaleLowerCase().indexOf(input.toLocaleLowerCase()) >= 0
        )
      },
      onSelect(value) {
        console.log('onSelect', value)
        getAction(this.url.fromProductSnToSkuCode, { productSn: value }).then(res => {
          if (res.success) {
            console.log('SkuCode => ' + res.message)
            this.form.setFieldsValue({ 'skuCode': res.message })
          } else {
            console.log(res.message)
            this.form.setFieldsValue({ 'skuCode': '' })
          }
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            //时间格式化
            formData.startTime = formData.startTime?formData.startTime.format('YYYY-MM-DD HH:mm:ss'):null;
            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.close()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
  }
</script>

<style lang="less" scoped>

</style>